#include "AEEAppGen.h"        // Applet helper file
#include "helloworld.bid"		// Applet-specific header that contains class ID
#include "nmdef.h"
#include "AEEMenu.h"

#include "resource.h"

#include "AEEStdLib.h"

#include "AEEText.h"

enum {
	WND_MAIN=0,
	WND_MAIN_MENU,
	WND_TEXT,
	WND_DRAW,
	WND_NETWORK
};


typedef struct _MainApp
{
	AEEApplet a ;

	IMenuCtl*	m_pIMainMenu;		// Pointer to Main menu control
	IMenuCtl*	m_pIEditMenu;		// Pointer to Main menu control

	ITextCtl*	m_pTextCtrl;

	uint16		m_nWndIdx;			//! 

}MainApp;

/*-------------------------------------------------------------------
Static function prototypes
-------------------------------------------------------------------*/
static boolean HelloWorld_HandleEvent(AEEApplet * pme, AEEEvent eCode,uint16 wParam, uint32 dwParam);
static void ShowMainMenu(MainApp* pMe);
boolean InitAppData(MainApp* pApp);
static void ShowTextMenu(MainApp* pMe);
static void ShowMenu1(MainApp* pMe);
static void ShowMenu2(MainApp* pMe);
static void ShowMenu3(MainApp* pMe);



int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * pMod,void ** ppObj)
{

	*ppObj = NULL;

	if(AEEApplet_New( sizeof(MainApp), ClsId, pIShell, pMod, (IApplet**)ppObj,
		(AEEHANDLER)HelloWorld_HandleEvent,  NULL) ) 
	{
		if(InitAppData((MainApp*)*ppObj))
		{
			//Data initialized successfully
			return(AEE_SUCCESS);
		}
		else
		{
			//Release the applet. This will free the memory allocated for the applet when
			// AEEApplet_New was called.
			IAPPLET_Release((IApplet*)*ppObj);
			return EFAILED;
		}

	}

	return(EFAILED);

}

boolean InitAppData(MainApp* pApp)
{
	MainApp* pMainApp = (MainApp*)pApp;

	// Initialize Data Members
	MEMSET(((char*)pMainApp) + sizeof(AEEApplet), 0, sizeof(*pMainApp) - sizeof(AEEApplet));

	pApp->m_nWndIdx = 0 ;


}


static boolean HelloWorld_HandleEvent(AEEApplet * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{  
	MainApp* pMainApp = (MainApp*)pMe;

	if (pMainApp->m_pTextCtrl)
	{
		ITEXTCTL_HandleEvent(pMainApp->m_pTextCtrl , eCode, wParam, dwParam);
	}


	switch (eCode){
	  case EVT_APP_START:                        
		  {

			  AECHAR szText[] = {'C','L','I','C','K','\:','C','\0'};
			  //AECHAR szText[] = {'3','e','l','l','o',' ','W','o', 'r', 'l', 'd', '\0'};

			  IDISPLAY_ClearScreen(pMe->m_pIDisplay);

			  IDISPLAY_DrawText(pMe->m_pIDisplay,    // Display instance
				  AEE_FONT_BOLD,       // Use BOLD font
				  szText,              // Text - Normally comes from resource
				  -1,                  // -1 = Use full string length
				  0,                   // Ignored - IDF_ALIGN_CENTER
				  0,                   // Ignored - IDF_ALIGN_MIDDLE
				  NULL,                // No clipping
				  IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);


			  IDISPLAY_Update (pMe->m_pIDisplay);

			  return(TRUE);
		  }
	  case EVT_APP_STOP:
		  return(TRUE);

	  case EVT_KEY:

		  if (wParam == AVK_CLR)
		  {
			  ShowMainMenu(pMainApp);
			  pMainApp->m_nWndIdx = WND_MAIN_MENU;
			  return TRUE;
		  }
		  //&& IMENUCTL_IsActive(pMainApp->m_pIMainMenu)
		  else if (pMainApp->m_pIMainMenu && pMainApp->m_nWndIdx == WND_MAIN_MENU)
		  {
			  if (IMENUCTL_HandleEvent(pMainApp->m_pIMainMenu, eCode, wParam, dwParam))
				  return TRUE;
		  }
		  else if (pMainApp->m_pIEditMenu && pMainApp->m_nWndIdx== WND_TEXT )
		  {
			  if (IMENUCTL_HandleEvent(pMainApp->m_pIEditMenu, eCode, wParam, dwParam))
				  return TRUE;
		  }




	  case EVT_COMMAND:
		  //if (pMainApp->m_pIMainMenu)
		  //{
		  //}
		  switch (wParam)
		  {
		  case IDS_STRING_1001:
			  ShowMenu1(pMainApp);
			  break;
		  case IDS_STRING_1002:
			  ShowMenu2(pMainApp);
			  break;
		  case IDS_STRING_1003:
			  ShowMenu3(pMainApp);
			  break;
		  case IDS_STRING_TEXTMENU1:
			  {
				  AECHAR userName[20];
				  ITEXTCTL_GetText(pMainApp->m_pTextCtrl , userName , 15);
				  userName[19] = 0;


			  }
			  break;

		  case IDS_STRING_TEXTMENU2:

			  break;


		  default:
			  break;
		  }

	  default:
		  break;
	}
	return(FALSE);
}


static void ShowMainMenu(MainApp *pMe)
{

	if (!pMe->m_pIMainMenu)
	{
		if (ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_MENUCTL, (void **)&(pMe->m_pIMainMenu)) == SUCCESS)
		{
			IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_STRING_1001, IDS_STRING_1001, NULL, NULL);
			IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_STRING_1002, IDS_STRING_1002, NULL, NULL);
			IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_STRING_1003, IDS_STRING_1003, NULL, NULL);

			// Automatically select the last used freeway as a convenience to the user
			IMENUCTL_SetSel(pMe->m_pIMainMenu, (uint16)1);
		}
	}

	if (pMe->m_pIMainMenu)
	{
		IMENUCTL_SetActive(pMe->m_pIMainMenu, TRUE);
		IMENUCTL_Redraw(pMe->m_pIMainMenu);
	}
}

//

static void ShowTextMenu(MainApp *pMe)
{

	if (!pMe->m_pIEditMenu)
	{
		if (ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_MENUCTL, (void **)&(pMe->m_pIEditMenu)) == SUCCESS)
		{

			IMENUCTL_AddItem(pMe->m_pIEditMenu, RESOURCE_RES_FILE, IDS_STRING_TEXTMENU1, IDS_STRING_TEXTMENU1, NULL, NULL);
			IMENUCTL_AddItem(pMe->m_pIEditMenu, RESOURCE_RES_FILE, IDS_STRING_TEXTMENU2, IDS_STRING_TEXTMENU2, NULL, NULL);

			// Automatically select the last used freeway as a convenience to the user
			IMENUCTL_SetSel(pMe->m_pIEditMenu, (uint16)1);
		}
	}

	if (pMe->m_pIMainMenu)
		IMENUCTL_SetActive(pMe->m_pIMainMenu, FALSE);

	if (pMe->m_pIEditMenu)
	{
		IMENUCTL_SetActive(pMe->m_pIEditMenu, TRUE);
		IMENUCTL_Redraw(pMe->m_pIEditMenu);
	}
}


static void ShowMenu1(MainApp* pMe)
{
	AECHAR szText[] = {'H','e','l','l','o',' ','W','o', 'r', 'l', 'd', '\0'};

	IDISPLAY_ClearScreen(pMe->a.m_pIDisplay);

	IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
		AEE_FONT_BOLD,       // Use BOLD font
		szText,              // Text - Normally comes from resource
		-1,                  // -1 = Use full string length
		0,                   // Ignored - IDF_ALIGN_CENTER
		0,                   // Ignored - IDF_ALIGN_MIDDLE
		NULL,                // No clipping
		IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);


	IDISPLAY_Update (pMe->a.m_pIDisplay);

	pMe->m_nWndIdx = WND_MAIN;

}


static void ShowMenu2(MainApp* pMe)
{
	//AECHAR* szText = "bbb";

	//IDISPLAY_ClearScreen(pMe->a.m_pIDisplay);

	//IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
	//	AEE_FONT_BOLD,       // Use BOLD font
	//	szText,              // Text - Normally comes from resource
	//	-1,                  // -1 = Use full string length
	//	0,                   // Ignored - IDF_ALIGN_CENTER
	//	0,                   // Ignored - IDF_ALIGN_MIDDLE
	//	NULL,                // No clipping
	//	IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);


	//IDISPLAY_Update (pMe->a.m_pIDisplay);




	AEERect rect;
	AEEApplet* pApp = (AEEApplet*)pMe;

	pMe->m_pIEditMenu = NULL;
	ISHELL_CreateInstance(pApp->m_pIShell , AEECLSID_SOFTKEYCTL , (void**)&pMe->m_pIEditMenu);
	IMENUCTL_AddItem(pMe->m_pIEditMenu, RESOURCE_RES_FILE, IDS_STRING_TEXTMENU1, IDS_STRING_TEXTMENU1, NULL, NULL);

	ISHELL_CreateInstance(pApp->m_pIShell , AEECLSID_TEXTCTL , (void**)&pMe->m_pTextCtrl);

	rect.x = 0;
	rect.y = 0;
	rect.dx = 100;
	rect.dy = 150;

	ITEXTCTL_SetProperties(pMe->m_pTextCtrl, TP_FRAME);
	//ITEXTCTL_SetTitle(pMe->m_pTextCtrl, );
	ITEXTCTL_SetRect(pMe->m_pTextCtrl, &rect);
	ITEXTCTL_SetMaxSize(pMe->m_pTextCtrl, 15);
	ITEXTCTL_SetSoftKeyMenu(pMe->m_pTextCtrl, pMe->m_pIEditMenu);
	ITEXTCTL_SetInputMode(pMe->m_pTextCtrl, AEE_TM_NUMBERS);
	ITEXTCTL_SetActive(pMe->m_pTextCtrl, TRUE);

	if (pMe->m_pIMainMenu)
		IMENUCTL_SetActive(pMe->m_pIMainMenu, FALSE);


	pMe->m_nWndIdx = WND_TEXT;






	//ShowTextMenu(pMe->m_pITextMenu , );



}

static void ShowMenu3(MainApp* pMe)
{
	AECHAR szText[] = {'3','e','l','l','o',' ','W','o', 'r', 'l', 'd', '\0'};

	IDISPLAY_ClearScreen(pMe->a.m_pIDisplay);

	IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
		AEE_FONT_BOLD,       // Use BOLD font
		szText,              // Text - Normally comes from resource
		-1,                  // -1 = Use full string length
		0,                   // Ignored - IDF_ALIGN_CENTER
		0,                   // Ignored - IDF_ALIGN_MIDDLE
		NULL,                // No clipping
		IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);


	IDISPLAY_Update (pMe->a.m_pIDisplay);

}
